import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Version,
  Query,
  Request,
  ParseBoolPipe,
  Ip,
} from "@nestjs/common";
import { UserService } from "./user.service";
import { CreateUserDto } from "./dto/create-user.dto";
import { UpdateUserDto } from "./dto/update-user.dto";

@Controller("user")
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Post()
  @Version("1")
  create(@Body() createUserDto: CreateUserDto) {
    console.log(createUserDto);
    return this.userService.create(createUserDto);
  }

  @Post()
  @Version("2")
  create2(@Request() req) {
    console.log(req.body);
    return {
      code: 200,
    };
  }

  @Get()
  @Version("1")
  findAll() {
    return this.userService.findAll();
  }

  @Get()
  @Version("2")
  findAll2(@Ip() ip: string) {
    return "version 2 find all from " + ip;
  }

  @Get()
  @Version("3")
  fundAll3(@Query("a", ParseBoolPipe) a: boolean, @Query() query) {
    console.log(query);
    return {
      code: 200,
      message: query.name,
      flag: a,
    };
  }

  @Get(":id")
  findOne(@Param("id") id: string) {
    return this.userService.findOne(+id);
  }

  @Patch(":id")
  update(@Param("id") id: string, @Body() updateUserDto: UpdateUserDto) {
    return this.userService.update(+id, updateUserDto);
  }

  @Delete(":id")
  remove(@Param("id") id: string) {
    return this.userService.remove(+id);
  }
}
